#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
用户数据传输对象

定义用户领域的请求和响应数据结构
"""

from typing import Dict, Any, List, Optional
from datetime import datetime
from pydantic import BaseModel, Field, EmailStr, validator


class CreateUserRequest(BaseModel):
    """创建用户请求DTO"""
    username: str = Field(..., min_length=3, max_length=50)
    password: str = Field(..., min_length=6, max_length=50)
    email: Optional[EmailStr] = None
    name: Optional[str] = Field(None, max_length=50)
    age: Optional[int] = Field(None, ge=0, le=120)
    gender: Optional[str] = Field(None, max_length=10)
    address: Optional[str] = Field(None, max_length=200)
    phone: Optional[str] = Field(None, max_length=20)
    
    @validator('username')
    def username_valid(cls, v):
        if not v.isalnum():
            raise ValueError('用户名只能包含字母和数字')
        return v


class UpdateUserRequest(BaseModel):
    """更新用户请求DTO"""
    email: Optional[EmailStr] = None
    name: Optional[str] = Field(None, max_length=50)
    age: Optional[int] = Field(None, ge=0, le=120)
    gender: Optional[str] = Field(None, max_length=10)
    address: Optional[str] = Field(None, max_length=200)
    phone: Optional[str] = Field(None, max_length=20)
    birthday: Optional[str] = None
    company: Optional[str] = Field(None, max_length=100)
    job: Optional[str] = Field(None, max_length=50)
    website: Optional[str] = Field(None, max_length=100)


class UserListRequest(BaseModel):
    """用户列表请求DTO"""
    page: int = Field(1, ge=1)
    size: int = Field(10, ge=1, le=100)
    username: Optional[str] = None
    email: Optional[str] = None


class BatchCreateUserRequest(BaseModel):
    """批量创建用户请求DTO"""
    count: int = Field(100, ge=1, le=500)
    password: str = Field("password123", min_length=6)


class UserResponse(BaseModel):
    """用户响应DTO"""
    user_id: str
    username: str
    email: Optional[str] = None
    name: Optional[str] = None
    age: Optional[int] = None
    gender: Optional[str] = None
    address: Optional[str] = None
    phone: Optional[str] = None
    birthday: Optional[str] = None
    company: Optional[str] = None
    job: Optional[str] = None
    website: Optional[str] = None
    created_time: str
    updated_time: str
    
    class Config:
        """配置"""
        orm_mode = True


class UserListResponse(BaseModel):
    """用户列表响应DTO"""
    total: int
    page: int
    size: int
    users: List[UserResponse]


class BatchCreateUserResponse(BaseModel):
    """批量创建用户响应DTO"""
    count: int
    error_count: int
    users: List[UserResponse] 